#include "manager.h"

// Controls the agglomeration process in 2D
// ==========================================================
// IN: Mesh, Volumes of CV, Edges Coefficients
// OUT: Sequence of Edge-Based Structures(agglomerated meshes)
void defineFileName(const char *meshFilename, char *filename, int iMesh);

void agg2D(pMesh theMesh, set<int> &setOfDomains, const char *meshFilename)
{

cout<<"----------------------------------------\n";
cout<<"Starting Agglomeration Process in 2D... \n";
cout<<"Agg-mg is back!! (04/04/10)\n";
// (DEV) - dados include  que devem vir atraves de arq. de dados
const int NMESH = 6;
//------------------------------------------------------

char filename[256];
vector<pMesh> aggMesh;

for (int iMesh=0; iMesh<NMESH; iMesh++)
{
    cout<<"NEW LEVEL...............................................\n";
	// Creating new mesh
	pMesh theNextMesh = MS_newMesh(0);
	aggMesh.push_back(theNextMesh);

	// Agglomerating control volumes
	aggCV(theMesh, setOfDomains, aggMesh, iMesh);

	// Creating super edges
	aggSE(theMesh, setOfDomains, aggMesh, iMesh);

	// Correcting special cases
	//aggSC(theMesh, setOfDomains, aggMesh, iMesh);

    // Testing mesh for errors
    aggTest(aggMesh.at(iMesh), setOfDomains);

	// Defining file name
	defineFileName(meshFilename, filename, iMesh);

	// Export coefficients
    exportCoefficients(aggMesh.at(iMesh),2,setOfDomains,filename);

    //Exporting .vtk
    outputPrimal(aggMesh.at(iMesh), 2, filename, iMesh, theMesh);

}
}

void defineFileName (const char *meshFilename, char *filename, int iMesh)
{
    string str(meshFilename);
    string::size_type start = str.find_last_of("/");
    start = (start == string::npos)?0:start;
    string::size_type end = str.find_last_of(".");
    char buffer[256];
    memset( buffer, '\0', 256 );
    str.copy(buffer,end-start,start);
    sprintf(filename,"preprocessor-datafiles/%s%d.dat",buffer,iMesh);
}
